package com.xueyi.aixihu.controller;

import com.xueyi.aixihu.controller.base.BaseController;
import com.xueyi.aixihu.entity.dto.AuditNewsDto;
import com.xueyi.aixihu.entity.dto.NewsDto;
import com.xueyi.aixihu.entity.po.DtNewsSort;
import com.xueyi.aixihu.entity.vo.*;
import com.xueyi.aixihu.exception.SzException;
import com.xueyi.aixihu.reqeust.*;
import com.xueyi.aixihu.reqeust.base.PageLimitReq;
import com.xueyi.aixihu.reqeust.base.Update;
import com.xueyi.aixihu.response.*;
import com.xueyi.aixihu.response.base.BaseResponse;
import com.xueyi.aixihu.service.DtNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @authoer:hxs
 * @createDate:2022/8/16
 * @description: 文章操作接口
 */
@RestController
@Validated
public class DtNewsController extends BaseController {


    @Autowired
    private DtNewsService dtNewsService;

    /**
     * 条件查询文章列表
     *
     * @param changeClassifyReq
     * @return
     */
//    @Aes
    @RequestMapping("/articleManage/findArticleList")
    public BaseResponse<ArticleListResponse> findArticleList(ChangeClassifyReq changeClassifyReq) throws SzException {
        ArticleListResponse response = dtNewsService.findArticleList(changeClassifyReq);
        return BaseResponse.success(response);
    }

    /**
     * 查询一个
     *
     * @param contentId
     * @return
     */
    @RequestMapping("/articleManage/findNewsByOne/{contentId}/{showType}")
    public BaseResponse<Map<String, Object>> findNewsByOne(@PathVariable Integer contentId,@PathVariable Integer showType) {
        Map<String, Object> newsByOne = dtNewsService.findNewsByOne(contentId,showType);
        return BaseResponse.success(newsByOne);
    }


    /**
     * 修改文章展示状态
     *
     * @param contentId
     * @return
     */
    @RequestMapping("/articleManage/removeNews/{contentId}")
    public BaseResponse<Boolean> updateArticleStatus(@PathVariable Integer contentId) throws SzException {
        dtNewsService.updateArticleStatus(contentId);
        return BaseResponse.success(Boolean.TRUE);
    }

    /**
     * 新增文章
     *
     * @param dtNewsReq
     * @return
     */
    @RequestMapping("/articleManage/addArticle")
    public BaseResponse<Boolean> addArticle(@Validated DtNewsReq dtNewsReq) throws SzException, ParseException {
        dtNewsService.addArticle(dtNewsReq);
        return BaseResponse.success(Boolean.TRUE);
    }

    /**
     * 编辑文章
     *
     * @param dtNewsReq
     * @return
     */
    @RequestMapping("/articleManage/updateArticle")
    public BaseResponse<Boolean> updateArticle(@Validated(Update.class) DtNewsReq dtNewsReq) throws SzException, ParseException {
        dtNewsService.updateArticle(dtNewsReq);
        return BaseResponse.success(Boolean.TRUE);
    }


    /**
     * 回显文章数据
     *
     * @param findArticleReq
     * @return
     */
//    @Aes
    @RequestMapping("/articleManage/findArticleById")
    public BaseResponse<ArticleVo> findArticleById(@Validated FindArticleReq findArticleReq) throws SzException {
        ArticleVo articleVo = dtNewsService.findArticleById(findArticleReq);
        return BaseResponse.success(articleVo);
    }


    /**
     * 获取微信文章并同步到数据库
     *
     * @param wxArticleReq
     * @return
     */
    @RequestMapping("/articleManage/findWxArticleFromWx")
    public BaseResponse<List<WxArticleVo>> findWxArticleFromWx(@Validated WxArticleReq wxArticleReq) throws SzException {
        List<WxArticleVo> wxArticle = dtNewsService.findWxArticleFromWx(wxArticleReq);
        return BaseResponse.success(wxArticle);
    }

    /**
     * 获取微信文章列表
     *
     * @param findWxArticlesReq
     * @return
     */
//    @Aes
    @RequestMapping("/articleManage/findWxArticles")
    public BaseResponse<WxArticlesResponse> findWxArticles(FindWxArticlesReq findWxArticlesReq) throws SzException {
        List<WxArticleVo> wxArticleVos = dtNewsService.findWxArticles(findWxArticlesReq);
        Integer total = dtNewsService.findWxArticlesTotal(findWxArticlesReq);
        WxArticlesResponse response = new WxArticlesResponse().wxArticleList(wxArticleVos).total(total);
        return BaseResponse.success(response);
    }

    /**
     * 修改微信文章的显示状态
     *
     * @param wxArticlesIsVisible
     * @return
     */
    @RequestMapping("/articleManage/updateWxArticlesIsVisible")
    public BaseResponse<Boolean> updateWxArticlesIsVisible(@Validated WxArticlesIsVisibleReq wxArticlesIsVisible) throws SzException {
        dtNewsService.updateWxArticlesIsVisible(wxArticlesIsVisible);
        return BaseResponse.success(Boolean.TRUE);
    }

    /**
     * 修改微信文章的所属社区
     *
     * @param updateWxArticleAreaReq
     * @return
     */
    @RequestMapping("/articleManage/updateWxArticleArea")
    public BaseResponse<Boolean> updateWxArticle(@Validated UpdateWxArticleAreaReq updateWxArticleAreaReq) throws SzException {
        dtNewsService.updateWxArticleArea(updateWxArticleAreaReq);
        return BaseResponse.success(Boolean.TRUE);
    }

    /**
     * 逻辑删除文章
     *
     * @param removeArticleAreaReq
     * @return
     */
    @RequestMapping("/articleManage/removeArticleArea")
    public BaseResponse<Boolean> removeArticleArea(@Validated RemoveArticleAreaReq removeArticleAreaReq) throws SzException {
        dtNewsService.removeArticleArea(removeArticleAreaReq);
        return BaseResponse.success(Boolean.TRUE);
    }

    /**
     * 获取党政新闻文章列表
     *
     * @return
     */
//    @Aes
    @RequestMapping("/articleManage/findPartyPolicyNews")
    public BaseResponse<FindPartyPolicyNewsResponse> findPartyPolicyNews(FindPartyPolicyNewsReq findPartyPolicyNewsReq) {
        FindPartyPolicyNewsResponse response = dtNewsService.findPartyPolicyNews(findPartyPolicyNewsReq);
        return BaseResponse.success(response);
    }

    /**
     * 获取用户的管辖范围
     *
     * @param type
     * @return
     * @throws SzException
     */
    @RequestMapping("/articleManage/findSort")
    public BaseResponse<List<DtNewsSort>> findSortList(Integer type) throws SzException {
        return BaseResponse.success(dtNewsService.findSortList(type));
    }


    /**
     * 通过pid查询管理的地址信息
     *
     * @param pid
     * @return
     */
    @RequestMapping("/findUserManageSortByPid")
    public BaseResponse<?> findUserManageSortByPid(Integer pid) {
        return BaseResponse.success(dtNewsService.findUserManageSortByPid(pid));
    }


    /**
     * 查询顶级的地址类别信息
     *
     * @param
     * @return
     */
    @RequestMapping("/findSortParent")
    public BaseResponse<?> findSortParent(Integer pageType) {
        if (pageType != null && pageType.equals(5)) {
            DtUsersVo currentUser = DtUsersVo.findCurrentUser();
            return BaseResponse.success(currentUser.findSortParent(currentUser.getUserMaxRole(),false));
        }
        return BaseResponse.success(dtNewsService.findSortParent());
    }

    /**
     * 获取type的列表
     *
     * @param type
     * @return
     * @throws SzException
     */
    @RequestMapping("/articleManage/findType")
    public BaseResponse<List<CdzSystemType>> findCdzContentTypeList(Integer type) {
        return BaseResponse.success(dtNewsService.findCdzType(type));
    }

    /**
     * 公开或者取消公开文章
     *
     * @param contentId
     * @param type
     * @return
     * @throws SzException
     */
    @RequestMapping("/articleManage/publicContent")
    public BaseResponse<Boolean> publicContent(Integer contentId, Integer type) throws SzException {
        return BaseResponse.success(dtNewsService.publicContent(contentId, type));
    }

    /**
     * 转换草稿
     * @param contentId
     */
    @RequestMapping("/articleManage/convertDrafts")
    public BaseResponse<String> convertDrafts(@NotNull Integer contentId,@NotNull Integer type) throws SzException{
        dtNewsService.convertDrafts(contentId,type);
        return BaseResponse.success("修改成功");
    }


    /**
     * 提交审核
     *
     * @param contentId
     * @param type
     * @return
     * @throws SzException
     */
    @RequestMapping("/articleManage/submitAudit")
    public BaseResponse<Boolean> submitAudit(Integer contentId, Integer type) throws SzException {
        return BaseResponse.success(dtNewsService.submitAudit(contentId, type));
    }

    /**
     * 查询文章数据
     *
     * @param type
     * @return
     */
    @RequestMapping("/articleManage/findContentData")
    public BaseResponse<ContentListDataVo> findContentData(Integer type) {
        return BaseResponse.success(dtNewsService.findContentData(type));
    }

    /**
     * 审核文章
     *
     * @param auditNewsDto
     * @return
     * @throws SzException
     */
    @RequestMapping("/news/audit-news")
    public BaseResponse<?> auditNews(AuditNewsDto auditNewsDto) throws SzException {

        dtNewsService.auditDtNews(auditNewsDto);
        return BaseResponse.success("OK");
    }


    /**
     * 文章审核列表
     *
     * @param changeClassifyReq
     * @return
     * @throws SzException
     */
    @RequestMapping("/news/audit-news-table")
    public BaseResponse<?> auditNewsTable(NewsDto changeClassifyReq) throws SzException {
        return BaseResponse.success(dtNewsService.auditNewsTable(changeClassifyReq));
    }

}
